/**
 * @param { url } 接口地址
 * @param { method } 请求方式
 * @param { data } 请求参数
 * @param { header } 请求头/token/cookie
 * @param { hideLoading }  显示加载动画
 * @dataType 解析为JSON
 *
 */

import Vue from 'vue';
var _this = Vue.prototype


const baseURL = "http://192.168.0.31:8080"


const request = (options) => {
    var hideLoading = options.hideLoading || false;
    const language = uni.getStorageSync('language') ?? 'zh-HK'; //多语言
    const cookie = 'xxxxx'; // 自己需要携带的Cookie内容
    if (!hideLoading) {
        uni.showLoading({
            title: _this.l('Loading'), // 这是我们的多语言写法
        })
    }

    return new Promise((resolve, reject) => {
        uni.request({
            url: baseURL + options.url,
            method: options.method || "GET",
            data: options.data || {},
            header: {
                'content-Type': 'application/json;charset=UTF-8',
                'UxSoft.Localization.CultureName': language, // 自定义请求头
                Cookie: cookie,
                token: uni.getStorageSync('userToken') ?? '',
            },
            dataType: 'json',
            success: res => {
                if (res.data.code === 403) {
                    let userInfo = uni.getStorageSync('userInfo'),
                        password = uni.getStorageSync('password'),
                        phoneNumber = uni.getStorageSync('phoneNumber'),
                        phoneAreaCode = uni.getStorageSync('phoneAreaCode');
                    if (userInfo  && password &&
                        phoneNumber && phoneAreaCode) {
                        _this.$request({
                            url: '/app/VipUserController/login',
                            method: 'GET',
                            hideLoading: true,
                            data: {
                                password: password,
                                phoneNumber: phoneNumber,
                                phoneAreaCode: phoneAreaCode
                            }
                        }).then(res => {
                            let routes = getCurrentPages() || [];
                            let currentRoute = routes[routes.length - 1]
                            if (res.data.result && res.data.result.token) {
                                uni.setStorageSync('userToken', res.data.result.token);
                                if (currentRoute) {
                                    uni.reLaunch({
                                        url: currentRoute.$page.fullPath,
                                        success(s) {
                                            console.log('登录重定向成功：', s)
                                        },
                                        fail(e) {
                                            console.log('登录重定向失败：', e)
                                        }
                                    })
                                } else {
                                    uni.reLaunch({
                                        url: '/platforms/mp-weixin/mine/mine'
                                    })
                                }
                            } else {
                                uni.showModal({
                                    title: _this.l('Tips'),
                                    content: res.data.error,
                                    showCancel: false,
                                    success: function(res) {
                                        uni.removeStorageSync('userInfo');
                                        uni.reLaunch({
                                            url: '/platforms/mp-weixin/discount/discount'
                                        })
                                    }
                                });
                            }
                        }).catch(err => console.log('重新登陆错误：', err))
                    } else {
                        uni.showToast({
                            title: _this.l('LoginExpiration'),
                            icon: 'none',
                            duration: 3000,
                        });
                        uni.removeStorageSync('userInfo');
                        setTimeout(() => {
                            uni.reLaunch({
                                url: '/platforms/mp-weixin/discount/discount'
                            })
                        }, 3000)
                        return;
                    }
                    reject();
                } else {
                    resolve(res)
                }
            },
            fail: (err) => {
                reject(err)
            },
            complete: () => {
                if (!hideLoading) {
                    uni.hideLoading()
                }
                resolve();
                return;
            }
        })
    })
}

export {
    baseURL,
    request
}
